﻿/**************************************************************
 *
 * 唯一标识：59962f78-7cb8-4682-83f3-27841841654a
 * 命名空间：Sgr.UPMS.Application.Commands.Organizations
 * 创建时间：2023/8/24 16:37:29
 * 机器名称：DESKTOP-S0D075D
 * 创建者：antho
 * 电子邮箱：fengqinhua2016@163.com
 * 描述：更新组织机构信息命令处理器
 *
 **************************************************************/

using MediatR;
using Sgr.Application.Commands;
using Sgr.UPMS.Domain.Organizations;
using System.Threading;
using System.Threading.Tasks;

namespace Sgr.UPMS.Application.Commands.Organizations
{
    /// <summary>
    /// 更新组织机构信息命令处理器
    /// </summary>
    /// <remarks>
    /// 负责处理组织机构基本信息的更新操作，包括名称、人员规模、类型、区域、联系人等信息
    /// </remarks>
    public class UpdateorgCommandHandle : IRequestHandler<UpdateOrgCommand, CommandResult<bool>>
    {
        private readonly IOrganizationManage _organizationManage;
        private readonly IOrganizationRepository _organizationRepository;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="organizationManage">组织机构管理器</param>
        /// <param name="organizationRepository">组织机构仓储</param>
        public UpdateorgCommandHandle(IOrganizationManage organizationManage, IOrganizationRepository organizationRepository)
        {
            _organizationManage = organizationManage;
            _organizationRepository = organizationRepository;
        }

        /// <summary>
        /// 处理更新组织机构信息命令
        /// </summary>
        /// <param name="request">包含更新信息的命令请求</param>
        /// <param name="cancellationToken">取消令牌</param>
        /// <returns>操作结果，包含成功标志</returns>
        public async Task<CommandResult<bool>> Handle(UpdateOrgCommand request, CancellationToken cancellationToken)
        {
            // 如果是自己关联自己，返回错误
            if (request.ParentId.HasValue && request.Id == request.ParentId.Value)
                return CommandResult.Invalid<bool>("组织机构不能关联自己作为上级");

            // 从仓储中获取组织机构实体
            var org = await _organizationRepository.GetAsync(request.Id, cancellationToken);

            // 如果未找到指定ID的组织机构，返回"查无数据"的结果
            if (org == null)
                return CommandResult.NotFound<bool>("未找到指定的组织机构");

            var moveResult = await _organizationManage.ValidateMoveOperationAsync(org, request.ParentId ?? 0, 5, cancellationToken);
            if (!moveResult.IsComply)
                return CommandResult.Invalid<bool>($"关联上级组织操作失败, {moveResult.Message}");

            // 更新组织机构的基本信息
            org.ChangeName(request.Name);
            org.StaffSizeCode = request.StaffSizeCode;
            org.Remarks = request.Remarks;

            // 更新组织机构的扩展信息
            org.OrgTypeCode = request.OrgTypeCode;
            org.AreaCode = request.AreaCode;
            org.Leader = request.Leader;
            org.Phone = request.Phone;
            org.Email = request.Email;
            org.Address = request.Address;
            org.OrderNumber = request.OrderNumber;

            // 保存更新后的组织机构到仓储
            await _organizationRepository.UpdateAsync(org, cancellationToken);

            // 返回成功结果
            return CommandResult.Success(true, "更新组织机构成功");
        }
    }
}